/**
 * Copyright 2013 Matt Holland <code.monkey.ota at gmail.com>
 *
 * This file is part of BotTender.
 *
 * BotTender is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * BotTender is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with BotTender. If not, see <http://www.gnu.org/licenses/>.
 */

package ota.ircbot.bottender;

import java.io.IOException;
import java.text.ParseException;
import java.util.Arrays;
import java.util.Iterator;
import java.util.List;

import org.pircbotx.PircBotX;
import org.pircbotx.User;
import org.pircbotx.exception.IrcException;
import org.pircbotx.exception.NickAlreadyInUseException;

import ota.ircbot.bottender.admin.*;
import ota.ircbot.config.BotConfig;


public class BotTender {

	static private BotConfig cfg;
	static private PircBotX bot;
	private static String nick;
	
	// VERSION reply
	private static String VERSION = "0.1.0";
	private static String AUTHOR = "Xek`ail \"Ota\" Saverem";
	
	private static void displayUsage() {
		
		System.out.println("###################### Usage ######################");
		System.out.println("BotTender.jar <Bot's Nickname> <Server[:port]> <Owner> [Bot's NS Password]");
		System.out.println("NOTE: This info is saved after the initial run, supplying it\nafterwards will override the previous information.");
		System.out.println("###################################################");
	}
	private static void firstUse(String[] args) { // This function gets called each time paramaters are supplied to the command line
		if(args.length < 3) {
			displayUsage();
			System.exit(1);
		}
		cfg = new BotConfig();
		cfg.setOption("Nick", args[0]);
		if(args[1].contains(":")) {
			String[] hostPort = args[1].split(":");
			cfg.setOption("Server", hostPort[0]);
			cfg.setOption("Port", Integer.decode(hostPort[1]));
		}
		else {
			cfg.setOption("Server", args[1]);
			cfg.setOption("Port", 6667);
		}
		
		cfg.setOption("Owner", args[2]);
		if(args.length == 4) cfg.setOption("NS", args[3]);

	}
	
	public static void main(String[] args) {
		
		if((cfg = (BotConfig) BotConfig.load()) == null || args.length > 2) firstUse(args);
		cfg.save();
		bot = new PircBotX();
		
		try {
			Authorized.setOwner(bot.getUser((String) cfg.getOption("Owner", String.class))); // Grab the user object for the owner
			
			//Listeners
			bot.getListenerManager().addListener(new ServeDrink(bot));
			bot.getListenerManager().addListener(new ShutdownComm());
			bot.getListenerManager().addListener(new ChannelComms(bot,cfg));
			
			nick = (String)cfg.getOption("Nick", String.class);
			bot.setName(nick);
			
			bot.setVerbose(true); // Debug
			bot.setVersion("C5 Bartending droid v" +VERSION+ " -- " +AUTHOR);
			bot.setFinger("Hail Ota!");
			
			bot.connect((String)cfg.getOption("Server", String.class), (Integer)cfg.getOption("Port",  Integer.class));
			
			List<String> chans = Arrays.asList(((String) cfg.getOption("Channels", String.class)).split(", "));
			Iterator<String> iter = chans.iterator();
			while(iter.hasNext()) {
				String aChan = iter.next();
				if(aChan != null) {
					bot.joinChannel(aChan);
					System.out.println("Joining " +aChan+ ".");	
				}
			}
			
		} catch (ParseException e) {
			System.out.println(e.getMessage());
		} catch (NickAlreadyInUseException e) {
			bot.changeNick(nick+"42");
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (IrcException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		cfg.save();
			
	}

}
